﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace MyAzureDashboard.Configuration
{
    public static class EncryptionHelper
    {
        private const string Key = "hcxilkqbbhczfeultgbskdmaunivmfuo";
        private const string RgbIV = "ryojvlzmdalyglrj";

        public static string Encrypt(string stringToEncrypt)
        {
            var stringToEncryptBytes = Encoding.UTF8.GetBytes(stringToEncrypt);
            var rijn = SymmetricAlgorithm.Create();
            var ms = new MemoryStream();
            var rgbIV = Encoding.ASCII.GetBytes(RgbIV);
            var key = Encoding.ASCII.GetBytes(Key);
            var cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);
            cs.Write(stringToEncryptBytes, 0, stringToEncryptBytes.Length);
            cs.Close();
            return Convert.ToBase64String(ms.ToArray());
        }

        public static string Decrypt(string stringToDecrypt)
        {
            var encryptedTextBytes = Convert.FromBase64String(stringToDecrypt);
            var ms = new MemoryStream();
            var rijn = SymmetricAlgorithm.Create();
            var rgbIV = Encoding.ASCII.GetBytes(RgbIV);
            var key = Encoding.ASCII.GetBytes(Key);
            var cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV), CryptoStreamMode.Write);
            cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
            cs.Close();
            return Encoding.UTF8.GetString(ms.ToArray());
        }
    }
}
